package com.ybox.common.core.serializer;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.BeanProperty;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.ContextualSerializer;
import com.ybox.common.core.annotation.PrivacyEncrypt;
import com.ybox.common.core.enums.PrivacyTypeEnum;
import com.ybox.common.core.text.PrivacyUtil;
import com.ybox.common.core.utils.StringUtils;
import java.io.IOException;
import java.util.Objects;


public class PrivacySerializer extends JsonSerializer<String> implements ContextualSerializer {

  // 脱敏类型
  private PrivacyTypeEnum privacyTypeEnum;
  // 前几位不脱敏
  private Integer prefixNoMaskLen;
  // 最后几位不脱敏
  private Integer suffixNoMaskLen;
  // 用什么打码
  private String symbol;

  public PrivacySerializer() {
    this.privacyTypeEnum = null;
    this.prefixNoMaskLen = null;
    this.suffixNoMaskLen = null;
    this.symbol = null;
  }

  public PrivacySerializer(PrivacyTypeEnum privacyTypeEnum, Integer prefixNoMaskLen, Integer suffixNoMaskLen, String symbol) {
    this.privacyTypeEnum = privacyTypeEnum;
    this.prefixNoMaskLen = prefixNoMaskLen;
    this.suffixNoMaskLen = suffixNoMaskLen;
    this.symbol = symbol;
  }

  @Override
  public void serialize(final String origin, final JsonGenerator jsonGenerator,
      final SerializerProvider serializerProvider) throws IOException {
    if (StringUtils.isNotBlank(origin) && null != privacyTypeEnum) {
      switch (privacyTypeEnum) {
        case CUSTOMER:
          jsonGenerator.writeString(PrivacyUtil.desValue(origin, prefixNoMaskLen, suffixNoMaskLen, symbol));
          break;
        case NAME:
          jsonGenerator.writeString(PrivacyUtil.hideChineseName(origin));
          break;
        case ID_CARD:
          jsonGenerator.writeString(PrivacyUtil.hideIDCard(origin));
          break;
        case PHONE:
          jsonGenerator.writeString(PrivacyUtil.hidePhone(origin));
          break;
        case EMAIL:
          jsonGenerator.writeString(PrivacyUtil.hideEmail(origin));
          break;
        default:
          throw new IllegalArgumentException("unknown privacy type enum " + privacyTypeEnum);
      }
    }
  }

  @Override
  public JsonSerializer<?> createContextual(final SerializerProvider serializerProvider,
      final BeanProperty beanProperty) throws JsonMappingException {
    if (beanProperty != null) {
      if (Objects.equals(beanProperty.getType().getRawClass(), String.class)) {
        PrivacyEncrypt privacyEncrypt = beanProperty.getAnnotation(PrivacyEncrypt.class);
        if (privacyEncrypt == null) {
          privacyEncrypt = beanProperty.getContextAnnotation(PrivacyEncrypt.class);
        }
        if (privacyEncrypt != null) {
          return new PrivacySerializer(privacyEncrypt.type(), privacyEncrypt.prefixNoMaskLen(),
              privacyEncrypt.suffixNoMaskLen(), privacyEncrypt.symbol());
        }
      }
      return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty);
    }
    return serializerProvider.findNullValueSerializer(null);
  }
}